﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpCopyToLevelDlg.h"
#include "GbmpNew.h"
#include "IElement.h"
#include "ILevel.h"
#include <QCheckBox>
#include <QLabel>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QSizePolicy>
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "IElementBasicInformation.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

//////////////////////////////////////////////////////////////////////////
GbmpLevelItemWidget::GbmpLevelItemWidget(const QString& itemName, bool isSourceLevel, QWidget *parent)
    : QWidget(parent)
    , m_pCheckBox(nullptr)
    , m_pLevelName(nullptr)
{
    gcmp::OwnerPtr<QCheckBox> opCheckBox =  NEW_AS_OWNER_PTR(QCheckBox," ");
    opCheckBox->setCheckState(Qt::Unchecked);
    opCheckBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    opCheckBox->setFixedSize(20, 20);
    QString qStr("CheckBox");
    opCheckBox->setObjectName(qStr + itemName);
    m_pCheckBox = opCheckBox.get();

    gcmp::OwnerPtr<QLabel> opLevelNameLabel =  NEW_AS_OWNER_PTR(QLabel, itemName);
    m_pLevelName = opLevelNameLabel.get();

    gcmp::OwnerPtr<QHBoxLayout> opLayout=  NEW_AS_OWNER_PTR(QHBoxLayout);
    opLayout->setSpacing(0);
    opLayout->setContentsMargins(0, 0, 0, 0);
    opLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opCheckBox), Qt::AlignLeft);
    opLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opLevelNameLabel), Qt::AlignLeft);
    
    if(isSourceLevel)
    {
        gcmp::OwnerPtr<QLabel> opLabel =  NEW_AS_OWNER_PTR(QLabel, QString::fromStdWString(GBMP_TR(L"(源标高)")));
        opLabel->setEnabled(false);
        opLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opLabel), Qt::AlignLeft);
    }
    opLayout->addItem(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(NEW_AS_OWNER_PTR(QSpacerItem, 40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum)));
    this->setLayout(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opLayout));
}

Qt::CheckState GbmpLevelItemWidget::GetCheckBoxState() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_pCheckBox, Qt::Unchecked, L"m_pCheckBox为空！",L"GDMPLab",L"2024-03-30");
    return m_pCheckBox->checkState();
}

void GbmpLevelItemWidget::SetCheckBoxState(Qt::CheckState checkState)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCheckBox, L"m_pCheckBox为空！",L"GDMPLab",L"2024-03-30");
    m_pCheckBox->setCheckState(checkState);
}

QString  GbmpLevelItemWidget::GetLevelName() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_pLevelName, QString(), L"m_pLevelName为空！",L"GDMPLab",L"2024-03-30");
    return m_pLevelName->text();
}
//////////////////////////////////////////////////////////////////////////

GbmpCopyToLevelDlg::GbmpCopyToLevelDlg(const gcmp::IDocument* pDoc, const gcmp::ILevel* pFromLevel, QWidget * pParent)
    : QDialog(pParent)
{
    ui.setupUi(this);
 
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowSystemMenuHint);
    this->setWindowTitle(QString::fromStdWString(GBMP_TR(L"选择标高")));
    std::vector<gcmp::IElement*> pLevels = pDoc->GetElementsByCategory(gcmp::BuiltInCategoryUniIdentities::BICU_NEW_ELEVATION);
    m_levelCount = pLevels.size();
    for(size_t index = 0; index < pLevels.size(); ++index)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLevels[index], L"pLevels[index]为空！",L"GDMPLab",L"2024-03-30");
        //过滤非本文档的标高
        if (pLevels[index]->GetDocument()!=pDoc)
        {
            continue;
        }

        gcmp::OwnerPtr<QListWidgetItem> opLevelItem = NEW_AS_OWNER_PTR(QListWidgetItem);
        bool isSourceLevel = false;
        if (pFromLevel)
        {
            isSourceLevel = (pLevels[index]->GetElementId() == pFromLevel->GetOwnerElement()->GetElementId());
        }
        gcmp::OwnerPtr<GbmpLevelItemWidget> opLevelItemWidget = NEW_AS_OWNER_PTR(GbmpLevelItemWidget, QString::fromStdWString(pLevels[index]->GetBasicInformation()->GetName()), isSourceLevel, this);;
        QString qStr("LevelItemWidget");
        opLevelItemWidget->setObjectName(qStr + QString::fromStdWString(pLevels[index]->GetBasicInformation()->GetName()));
        QListWidgetItem* pLevelItem = TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opLevelItem);
        ui.listWidget->addItem(pLevelItem);
        ui.listWidget->setItemWidget(pLevelItem, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opLevelItemWidget));
    }

    ui.listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
    Init();

}

void GbmpCopyToLevelDlg::Init()
{
    // 连接信号与信号槽
    connect(ui.okPushButton, SIGNAL(clicked()), this, SLOT(OnOkButtonPressed()));
    connect(ui.cancelPushButton, SIGNAL(clicked()), this, SLOT(OnCancelButtonPressed()));
    connect(ui.listWidget, SIGNAL(clicked(const QModelIndex & )), this, SLOT(OnListWidgetClicked(const QModelIndex &)));
}

void GbmpCopyToLevelDlg::OnOkButtonPressed()
{
    for(size_t index = 0; index < m_levelCount; ++index)
    {
        QListWidgetItem * widgetItem = ui.listWidget->item((int)index);
        DBG_WARN_AND_RETURN_VOID_UNLESS(widgetItem, L"widgetItem为空！",L"GDMPLab",L"2024-03-30");
        QWidget * item = ui.listWidget->itemWidget(widgetItem);
        GbmpLevelItemWidget* itemWidget = dynamic_cast<GbmpLevelItemWidget*>(item);
        DBG_WARN_AND_RETURN_VOID_UNLESS(itemWidget, L"itemWidget为空！",L"GDMPLab",L"2024-03-30");
        if(itemWidget->GetCheckBoxState() == Qt::Checked)
            m_exchangeData.selectedLevels.insert(itemWidget->GetLevelName().toStdWString());
    }
    QDialog::accept();
}

void GbmpCopyToLevelDlg::OnCancelButtonPressed()
{
    QDialog::reject();
}

void GbmpCopyToLevelDlg::OnListWidgetClicked(const QModelIndex & index)
{
    QModelIndexList indexList = ui.listWidget->selectionModel()->selectedIndexes();
    if(indexList.size() == 1)
    {
        QListWidgetItem * widgetItem = ui.listWidget->item(indexList.begin()->row());
        DBG_WARN_AND_RETURN_VOID_UNLESS(widgetItem, L"widgetItem为空！",L"GDMPLab",L"2024-03-30");
        QWidget * item = ui.listWidget->itemWidget(widgetItem);
        GbmpLevelItemWidget* itemWidget = dynamic_cast<GbmpLevelItemWidget*>(item);
        DBG_WARN_AND_RETURN_VOID_UNLESS(itemWidget, L"itemWidget为空！",L"GDMPLab",L"2024-03-30");
        if(itemWidget->GetCheckBoxState() == Qt::Checked)
        {
            itemWidget->SetCheckBoxState(Qt::Unchecked);
        }
        else
        {
            itemWidget->SetCheckBoxState(Qt::Checked);
        }
    }
    else if(indexList.size() > 1)
    {
        bool isAllSelected = true;
        FOR_EACH(index, indexList)
        {
            QListWidgetItem * widgetItem = ui.listWidget->item(index.row());
            DBG_WARN_AND_RETURN_VOID_UNLESS(widgetItem, L"widgetItem为空！",L"GDMPLab",L"2024-03-30");
            QWidget * item = ui.listWidget->itemWidget(widgetItem);
            GbmpLevelItemWidget* itemWidget = dynamic_cast<GbmpLevelItemWidget*>(item);
            DBG_WARN_AND_RETURN_VOID_UNLESS(itemWidget, L"itemWidget为空！",L"GDMPLab",L"2024-03-30");
            if(itemWidget->GetCheckBoxState()  == Qt::Unchecked)
            {
                isAllSelected = false;
                break;
            }
        }
        FOR_EACH(index, indexList)
        {
            QListWidgetItem * widgetItem = ui.listWidget->item(index.row());
            DBG_WARN_AND_RETURN_VOID_UNLESS(widgetItem, L"widgetItem为空！",L"GDMPLab",L"2024-03-30");
            QWidget * item = ui.listWidget->itemWidget(widgetItem);
            GbmpLevelItemWidget* itemWidget = dynamic_cast<GbmpLevelItemWidget*>(item);
            if(isAllSelected)
                itemWidget->SetCheckBoxState(Qt::Unchecked);
            else
                itemWidget->SetCheckBoxState(Qt::Checked);
        }
    }

}
